package com.weipai.springboot.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransOrderQueryModel;
import com.alipay.api.domain.AlipayFundTransToaccountTransferModel;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayFundTransOrderQueryRequest;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayFundTransOrderQueryResponse;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.alipay.api.response.AlipayTradeAppPayResponse;


import java.util.Map;

/**
 *支付宝 付款
 */
public class AliPayUtil {

    /*APP私钥*/
    private final static String APP_PRIVATE_KEY = PropUtils.getString("APP_PRIVATE_KEY");
    /*APP公钥*/
    private final static String APP_PUBLIC_KEY = PropUtils.getString("APP_PUBLIC_KEY");
    /*支付宝公钥*/
    public final static String ALIPAY_PUBLIC_KEY = PropUtils.getString("ALIPAY_PUBLIC_KEY");
    private final static String APP_ID = PropUtils.getString("APP_ID");
    private final static String ALIPAY_URL = "https://openapi.alipay.com/gateway.do";
    private final static String ALIPAY_FORMAT = "json";
    private final static String ALIPAY_CHARSET = "UTF-8";
    private final static String SIGN_TYPE = "RSA2";
    public final static String NOTIFY_URL = PropUtils.getString("NOTIFY_URL");

    public static AlipayClient alipayClient = new DefaultAlipayClient(ALIPAY_URL, APP_ID, APP_PRIVATE_KEY, ALIPAY_FORMAT, ALIPAY_CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

    public static String creatSign(AlipayTradeAppPayModel model){
        if(model!=null){
            //实例化客户端
            AlipayClient alipayClient = new DefaultAlipayClient(ALIPAY_URL, APP_ID, APP_PRIVATE_KEY, ALIPAY_FORMAT, ALIPAY_CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
            //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
            AlipayTradeAppPayRequest aliRequest = new AlipayTradeAppPayRequest();
            //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
            aliRequest.setBizModel(model);
            aliRequest.setNotifyUrl(NOTIFY_URL);
            //这里和普通的接口调用不同，使用的是sdkExecute
            try {
                AlipayTradeAppPayResponse aliResponse = alipayClient.sdkExecute(aliRequest);
                String sign = aliResponse.getBody();
                return sign;
            } catch (AlipayApiException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    /**
     * 单笔转账到支付宝账户
     * https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.54Ty29&treeId=193&articleId=106236&docType=1
     * @return
     * @throws AlipayApiException
     */
    public static boolean transfer(AlipayFundTransToaccountTransferModel model) throws AlipayApiException{
        AlipayFundTransToaccountTransferResponse response = transferToResponse(model);
        String result = response.getBody();
        System.out.println("transfer result>"+result);
        if (response.isSuccess()) {
            return true;
        } else {
            //调用查询接口查询数据
            JSONObject jsonObject = JSONObject.parseObject(result);
            String out_biz_no = jsonObject.getJSONObject("alipay_fund_trans_toaccount_transfer_response").getString("out_biz_no");
            AlipayFundTransOrderQueryModel queryModel = new AlipayFundTransOrderQueryModel();
            model.setOutBizNo(out_biz_no);
            boolean isSuccess = transferQuery(queryModel);
            if (isSuccess) {
                return true;
            }
        }
        return false;
    }
    

    public static AlipayFundTransToaccountTransferResponse transferToResponse(AlipayFundTransToaccountTransferModel model) throws AlipayApiException{
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        request.setBizModel(model);
        return alipayClient.execute(request);
    }

    /**
     * 转账查询接口
     * @return
     * @throws AlipayApiException
     */
    public static boolean transferQuery(AlipayFundTransOrderQueryModel model) throws AlipayApiException{
        
        AlipayFundTransOrderQueryResponse response = transferQueryToResponse(model);
        System.out.println("transferQuery result>"+response.getBody());
        if(response.isSuccess()){
            return true;
        }
        return false;
    }
    public static AlipayFundTransOrderQueryResponse transferQueryToResponse(AlipayFundTransOrderQueryModel model) throws AlipayApiException{
        AlipayFundTransOrderQueryRequest request = new AlipayFundTransOrderQueryRequest();
        request.setBizModel(model);
        return alipayClient.execute(request);
    }
    
    public static String invoking(Map<String, String> map){
        AlipayClient alipayClient = new DefaultAlipayClient(ALIPAY_URL, APP_ID, APP_PRIVATE_KEY, ALIPAY_FORMAT, ALIPAY_CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        request.setBizContent("{" +
                "    \"out_biz_no\":\"3142321423432\"," +
                "    \"payee_type\":\"ALIPAY_LOGONID\"," +
                "    \"payee_account\":\"abc@sina.com\"," +
                "    \"amount\":\"12.23\"," +
                "    \"payer_show_name\":\"上海交通卡退款\"," +
                "    \"payee_real_name\":\"张三\"," +
                "    \"remark\":\"转账备注\"," +
                "  }");
        try{
            AlipayFundTransToaccountTransferResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }
        }catch(AlipayApiException e){
            e.printStackTrace();
        }
        return null;
    }
    
}
